package njust.cms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import njust.tables.admin.TbAdminDAO;
import njust.tables.boardroom.TbBoardroom;
import njust.tables.boardroom.TbBoardroomDAO;
import njust.tables.order.TbOrder;
import njust.tables.order.TbOrderDAO;
import njust.tables.student.TbStudent;
import njust.tables.student.TbStudentDAO;
import njust.tables.teacher.TbTeacher;
import njust.tables.teacher.TbTeacherDAO;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/admin")
public class Admin {
	@Resource(name = "TbStudentDAO")
	private TbStudentDAO studentdao;

	@Resource(name = "TbTeacherDAO")
	private TbTeacherDAO teacherdao;

	@Resource(name = "TbAdminDAO")
	private TbAdminDAO admindao;

	@Resource(name = "TbBoardroomDAO")
	private TbBoardroomDAO boardroomdao;
	
	@Resource(name = "TbOrderDAO")
	private TbOrderDAO orderdao;

	@RequestMapping("example")
	public String example(HttpServletRequest request) {
		return "example";
	}

	@RequestMapping("/main")
	public String index(HttpServletRequest request) {
		List<TbBoardroom> myroom = boardroomdao.findAll();
		request.setAttribute("myroom", myroom);
		List<TbStudent> mystudent = studentdao.findAll();
		request.setAttribute("mystudent", mystudent);
		List<TbTeacher> myteacher = teacherdao.findAll();
		request.setAttribute("myteacher", myteacher);
		return "main_admin";
	}

	@RequestMapping("/room_list")
	// 列出会议室列表
	public String room_list(HttpServletRequest request) {
		List<TbBoardroom> myroom = boardroomdao.findAll();
		request.setAttribute("myroom", myroom);
		return "admin_room_list";
	}

	@RequestMapping("/room_add")
	public String room_add(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String flag = "0";
		if (session.getAttribute("room_flag") != null
				&& flag.equals((String) session.getAttribute("room_flag")))
			request.setAttribute("room_flag", "0");
		else
			request.setAttribute("room_flag", "1");
		session.setAttribute("room_flag", "1");// 重置查重标志位
		return "admin_room_add";
	}

	@RequestMapping("/room/submit")
	// 添加新会议室
	public String room_submit(HttpServletRequest request, String b_id,
			Integer b_size, Integer media, String b_address, String remarks) {
		if (boardroomdao.findByBId(b_id).size() == 0)// 查重
		{
			TbBoardroom newroom = new TbBoardroom();
			newroom.setBId(b_id);
			newroom.setBSize(b_size);
			newroom.setMedia(media);
			newroom.setBAddress(b_address);
			newroom.setRemarks(remarks);
			boardroomdao.save(newroom);
			return "redirect:/admin/room_list";
		} else {
			HttpSession session = request.getSession();
			session.setAttribute("room_flag", "0");// 查重标志
			session.setAttribute("room_num", b_id);
			return "redirect:/admin/room_add";
		}
	}

	// 列出学生列表
	@RequestMapping("/student_list")
	public String student_list(HttpServletRequest request) {
		List<TbStudent> mystudent = studentdao.findAll();
		request.setAttribute("mystudent", mystudent);
		return "admin_student_list";
	}

	@RequestMapping("/student_register")
	public String student_register(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String flag = "0";
		if (session.getAttribute("stu_flag") != null
				&& flag.equals((String) session.getAttribute("stu_flag")))
			request.setAttribute("stu_flag", "0");
		else
			request.setAttribute("stu_flag", "1");
		session.setAttribute("stu_flag", "1");// 重置查重标志位
		return "admin_student_register";
	}

	// 注册学生
	@RequestMapping("/student_submit")
	public String student_submit(HttpServletRequest request, String user_id,
			String user_name, Integer gender, String college, Integer grade,
			String tel, String email) {
		if (studentdao.findByUserId(user_id).size() == 0
				&& teacherdao.findByUserId(user_id).size() == 0
				&& admindao.findByAdminId(user_id).size() == 0)// 查重
		{
			TbStudent newstudent = new TbStudent();
			newstudent.setUserId(user_id);
			newstudent.setUserName(user_name);
			newstudent.setPassword("123456");
			newstudent.setGender(gender);
			newstudent.setGrade(grade);
			newstudent.setCollege(college);
			newstudent.setTel(tel);
			newstudent.setEmail(email);
			studentdao.save(newstudent);
			return "redirect:/admin/student_list";
		} else {
			HttpSession session = request.getSession();
			session.setAttribute("stu_flag", "0");// 查重标志
			session.setAttribute("stu_id", user_id);
			return "redirect:/admin/student_register";
		}
	}

	// 列出教师列表
	@RequestMapping("/teacher_list")
	public String teacher_list(HttpServletRequest request) {
		List<TbTeacher> myteacher = teacherdao.findAll();
		request.setAttribute("myteacher", myteacher);
		return "admin_teacher_list";
	}

	@RequestMapping("/teacher_register")
	public String teacher_register(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String flag = "0";
		if (session.getAttribute("teacher_flag") != null
				&& flag.equals((String) session.getAttribute("teacher_flag")))
			request.setAttribute("teacher_flag", "0");
		else
			request.setAttribute("teacher_flag", "1");
		session.setAttribute("teacher_flag", "1");// 重置查重标志位
		return "admin_teacher_register";
	}

	// 注册教师
	@RequestMapping("/teacher_submit")
	public String teacher_submit(HttpServletRequest request, String user_id,
			String user_name, Integer gender, String college, String tel,
			String email) {
		if (studentdao.findByUserId(user_id).size() == 0
				&& teacherdao.findByUserId(user_id).size() == 0
				&& admindao.findByAdminId(user_id).size() == 0)// 查重
		{
			TbTeacher newteacher = new TbTeacher();
			newteacher.setUserId(user_id);
			newteacher.setUserName(user_name);
			newteacher.setPassword("123456");
			newteacher.setGender(gender);
			newteacher.setCollege(college);
			newteacher.setTel(tel);
			newteacher.setEmail(email);
			teacherdao.save(newteacher);
			return "redirect:/admin/teacher_list";
		} else {
			HttpSession session = request.getSession();
			session.setAttribute("teacher_flag", "0");// 查重标志
			session.setAttribute("teacher_id", user_id);
			return "redirect:/admin/teacher_register";
		}
	}

	@RequestMapping("/student_delete")
	public String student_delete(HttpServletRequest request, Integer id) {
		TbStudent stu = studentdao.findById(id);
		if (stu != null)
			studentdao.delete(stu);
		return "redirect:/admin/student_list";
	}

	@RequestMapping("/teacher_delete")
	public String teacher_delete(HttpServletRequest request, Integer id) {
		TbTeacher t = teacherdao.findById(id);
		if (t != null)
			teacherdao.delete(t);
		return "redirect:/admin/teacher_list";
	}
	
	@RequestMapping("/room_delete")
	public String room_delete(HttpServletRequest request, Integer id) {
		TbBoardroom r = boardroomdao.findById(id);
		if (r != null)
			boardroomdao.delete(r);
		return "redirect:/admin/room_list";
	}

	@RequestMapping("/student_modify")
	public String student_modify(HttpServletRequest request, Integer id) {
		TbStudent stu = studentdao.findById(id);
		request.setAttribute("stu", stu);
		return "admin_student_modify";
	}

	@RequestMapping("/student_modify_submit")
	public String student_modify_submit(HttpServletRequest request, Integer id,
			String user_name, Integer gender, String college, Integer grade,
			String tel, String email) {
		TbStudent temp = studentdao.findById(id);
		temp.setUserName(user_name);
		temp.setGender(gender);
		temp.setGrade(grade);
		temp.setCollege(college);
		temp.setTel(tel);
		temp.setEmail(email);
		studentdao.merge(temp);
		return "redirect:/admin/student_list";
	}

	@RequestMapping("/teacher_modify")
	public String teacher_modify(HttpServletRequest request, Integer id) {
		TbTeacher t = teacherdao.findById(id);
		request.setAttribute("t", t);
		return "admin_teacher_modify";
	}

	@RequestMapping("/teacher_modify_submit")
	public String teacher_modify_submit(HttpServletRequest request, Integer id,
			String user_name, Integer gender, String college, String tel,
			String email) {
		TbTeacher temp = teacherdao.findById(id);
		temp.setUserName(user_name);
		temp.setGender(gender);
		temp.setCollege(college);
		temp.setTel(tel);
		temp.setEmail(email);
		teacherdao.merge(temp);
		return "redirect:/admin/teacher_list";
	}

	@RequestMapping("/room_modify")
	public String room_modify(HttpServletRequest request, Integer id) {
		TbBoardroom r = boardroomdao.findById(id);
		request.setAttribute("room", r);
		return "admin_room_modify";
	}

	@RequestMapping("/room_modify_submit")
	public String room_modify_submit(HttpServletRequest request, Integer id,
			Integer b_size, Integer media, String remarks,
			String b_address) {
		TbBoardroom temp = boardroomdao.findById(id);
		temp.setBSize(b_size);
		temp.setMedia(media);
		temp.setRemarks(remarks);
		temp.setBAddress(b_address);
		boardroomdao.merge(temp);
		return "redirect:/admin/room_list";
	}
	
	@RequestMapping("/room_bookinfo")
	public String room_bookinfo(HttpServletRequest request, String id) {
		List<TbOrder> booklist = orderdao.findByBId(id);
		request.setAttribute("roomid", id);
		request.setAttribute("bookinfo", booklist);
		return "admin_room_bookinfo";
	}
	
	@RequestMapping("/room_orderinfo")
	public String orderinfo(HttpServletRequest request, Integer id) {
		TbOrder orderinfo = orderdao.findById(id);
		
		String userid = orderinfo.getUserId();
		List<TbStudent> liststu = studentdao.findByUserId(userid);
		String username = "";
		if(liststu.size() > 0)
		{
			TbStudent stu = liststu.get(0);
			username = stu.getUserName();
		}
		else
		{
			TbTeacher teac = (TbTeacher) teacherdao.findByUserId(userid).get(0);
			username = teac.getUserName();
		}
		request.setAttribute("username", username);
		request.setAttribute("roomid", orderinfo.getBId());
		request.setAttribute("orderinfo", orderinfo);
		return "admin_room_orderinfo";
	}
	
	@RequestMapping("/room_update")
	public String room_update(HttpServletRequest request, Integer id,Integer isagree,String backreason) {
		TbOrder orderinfo = orderdao.findById(id);
		if(isagree == 0)
		{
			orderinfo.setState(2);  //1代表未审核，2代表审核未通过，3代表审核通过
			orderinfo.setApproved(0);
			orderinfo.setBackReason(backreason);
		}
		else
		{
			orderinfo.setState(3);
			orderinfo.setApproved(1);
			orderinfo.setBackReason("");
		}
		orderdao.merge(orderinfo);
		
		return "redirect:/admin/room_list";
	}
}
